<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';

header('Content-Type: text/plain');

$tableProduce = 'ns_Produce';
$tableProduceEvent = 'ns_ProduceEvent';
$tableProduceItemMail = 'ns_ProduceItemMail';
$tableProduceItemUser = 'ns_ProduceItemUser';
$tableProduceProduct = 'ns_ProduceProduct';
$tableProducePurchaseItemSelect = 'ns_ProducePurchaseItemSelect';
$tableProduceSelect = 'ns_ProduceSelect';
$tableProducePurchaseItem = 'ns_ProducePurchaseItem';
$tableProduceTrack = 'ns_ProduceTrack';
$tableProduceTransition = 'ns_ProduceTransition';
$tableProduceUser = 'ns_ProduceUser';
$tableProduceNote = 'ns_ProduceNote';

try {
  $hostname = HOST_NAME;
  $username = USER_NAME2;
  $password = PASSWORD2;
  $dbname = DATABASE_NAME2;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}

$action = MiscUtils::getParam('action', NULL);

try {
  switch ($action) {
    case 'produceevent':
      produceevent($myPdo);
      break;
    case 'events':
      events($myPdo);
      break;
    case 'findByRequest':
      findByRequest($myPdo);
      break;
    case 'overduetasks':
      overduetasks($myPdo);
      break;
    case 'overduetask':
      overduetask($myPdo);
      break;
    case 'smry':
      smry($myPdo);
      break;
    case 'smrydeadline':
      smrydeadline($myPdo);
      break;
    case 'producenote':
      producenote($myPdo);
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}


function produceevent($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProducePurchaseItemSelect, $tableProduceSelect, $tableProducePurchaseItem, $tableProduceTrack, $tableProduceTransition, $tableProduceUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 4);
  $pageSkip = ($page - 1) * $size;
    
  $operator = json_decode(MiscUtils::getParam('operator', NULL));
  $branch = json_decode(MiscUtils::getParam('b', ''));
  
  if ($branch == '') {
    $bran = '';
  } else {
    $bran = ' AND p.branch = \''.$branch.'\'';
  }
  
  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT e.e_oid as id, e.date as date, e.userAccount as userAccount, e.sourceId as sourceId, e.targetId as targetId, e.extra as extra, e.overdueReason, p.e_oid as produceId, p.trackId as trackId, p.customerId as customerId, p.isCustom, p.orderTrackId as orderTrackId, p.orderFlag as orderFlag, p.orderId as orderId, p.state as state, p.title as title, p.branch as branch, p.user_account, p.pi_quantity, p.pi_trackId, p.pi_total, p.pi_packageInc, p.pi_sampleId, p.pi_name, p.pi_price, p.pi_unit, p.pi_type, p.pi_model, p.pi_otherId FROM $tableProduceEvent AS e LEFT JOIN $tableProduce AS p ON p.trackId = e.produce_trackId $condition AND p.cancelled = 0 AND e.userAccount = '$operator->account' $bran ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->date = SimpleDate::fromStamp($tmp->date);

    $tmp->pUsers = array();
    $sql7 = "SELECT DISTINCT pu.e_oid as id, pu.account as account FROM $tableProduceUser AS pu  WHERE pu.produce_id = $tmp->produceId";
    $stmt7 = $myPdo->prepare($sql7);
    $stmt7->execute();
    $n = 0; 
    while ($n < $stmt7->rowCount()) {
      $tmp7 = $stmt7->fetch(PDO::FETCH_OBJ);
      array_push($tmp->pUsers, $tmp7->account);
      $n++;
    }
    
    $tmp->pi_selects = array();
    $sql5 = "SELECT DISTINCT tpps.e_oid as id, tpps.title as title, tpps.value as value FROM $tableProduceSelect AS tpps  WHERE tpps.produce_trackId = '$tmp->trackId'";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $m = 0;
    while ($m < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      array_push($tmp->pi_selects, $tmp5);
      $m++;
    }
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function events($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProducePurchaseItemSelect, $tableProducePurchaseItem, $tableProduceSelect, $tableProduceTrack, $tableProduceTransition, $tableProduceUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (e.date >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (e.date <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $branch = json_decode(MiscUtils::getParam('b', ''));
  
  if ($branch == '') {
    $bran = '';
  } else {
    $bran = ' AND p.branch = \''.$branch.'\'';
  }
  
  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT e.e_oid AS total FROM $tableProduceEvent AS e LEFT JOIN $tableProduce AS p ON p.trackId = e.produce_trackId WHERE p.cancelled = 0 AND $condition $bran";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT DISTINCT e.e_oid as id, e.date as date, e.userAccount as userAccount, e.sourceId as sourceId, e.targetId as targetId, e.extra as extra, e.overdueReason, p.e_oid as produceId, p.trackId as trackId, p.customerId as customerId, p.isCustom, p.orderId as orderId, p.orderTrackId as orderTrackId, p.orderFlag as orderFlag, p.state as state, p.title as title, p.branch as branch, p.user_account, p.pi_quantity, p.pi_trackId, p.pi_total, p.pi_packageInc, p.pi_sampleId, p.pi_name, p.pi_price, p.pi_unit, p.pi_type, p.pi_model, p.pi_otherId FROM $tableProduceEvent AS e LEFT JOIN $tableProduce AS p ON p.trackId = e.produce_trackId WHERE p.cancelled = 0 AND $condition $bran ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->date = SimpleDate::fromStamp($tmp->date);

    $tmp->pUsers = array();
    $sql7 = "SELECT DISTINCT pu.e_oid as id, pu.account as account FROM $tableProduceUser AS pu WHERE pu.produce_id = $tmp->produceId";
    $stmt7 = $myPdo->prepare($sql7);
    $stmt7->execute();
    $n = 0; 
    while ($n < $stmt7->rowCount()) {
      $tmp7 = $stmt7->fetch(PDO::FETCH_OBJ);
      array_push($tmp->pUsers, $tmp7->account);
      $n++;
    }

    $tmp->pi_selects = array();
    $sql5 = "SELECT DISTINCT tpps.e_oid as id, tpps.title as title, tpps.value as value FROM $tableProduceSelect AS tpps  WHERE tpps.produce_trackId = '$tmp->trackId'";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $m = 0;
    while ($m < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      array_push($tmp->pi_selects, $tmp5);
      $m++;
    }
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function findByRequest($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $result = new stdClass();
  $result->data = array();
  
  $sql = MiscUtils::getParam('sql', '');
  if ($sql) {
    try {
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $i = 0;
      while ($i < $stmt->rowCount()) {
        $tmp = $stmt->fetch(PDO::FETCH_OBJ);
        $result->data[] = $tmp;
        $i++;
      }
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}


function overduetasks($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT e.e_oid AS total FROM $tableProduceEvent AS e LEFT JOIN $tableProduce AS p ON p.trackId = e.produce_trackId WHERE p.cancelled = 0 AND e.isExpired = 1 AND $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT e.e_oid as id, e.date as date, e.userAccount as userAccount, e.sourceId as sourceId, e.extra as extra, e.admAppr as admAppr, e.isExpired as isExpired, e.overdueReason as overdueReason, p.e_oid as produceId, p.trackId as trackId, p.customerId as customerId, p.orderId as orderId, p.orderTrackId as orderTrackId, p.orderFlag as orderFlag, p.state as state, p.title as title, p.branch as branch, p.pi_model FROM $tableProduceEvent AS e LEFT JOIN $tableProduce AS p ON p.trackId = e.produce_trackId WHERE p.cancelled = 0 AND e.isExpired = 1 AND $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->date = ($tmp->date) ? SimpleDate::fromStamp($tmp->date) : NULL;
    
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function overduetask($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser;

  $events = json_decode(MiscUtils::getParam('e', NULL));

  foreach($events as &$event) {
    $sql = "UPDATE $tableProduceEvent set admAppr = 1 WHERE e_oid = '$event'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function smry($myPdo) {
  global $tableProduce;
  
  $args = MiscUtils::getParam('a', NULL);
  $args = ($args) ? json_decode($args) : array();
  $branch = json_decode(MiscUtils::getParam('b', NULL));
  
  if ($branch) {
    $br = " AND (d.branch = '$branch')";
  } else {
   $br = '';
  }
  
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;
  $date = SimpleDate::toStamp($now);
  
  $smry = array();
  foreach ($args as &$arg) {
    try {
      $smry[$arg->name] = array();

      $sql = "SELECT DISTINCT d.e_oid as id FROM $tableProduce AS d WHERE (" . $arg->cond . ')'. $br . "";
      
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $smry[$arg->name]['total'] = $stmt->rowCount();
      
      $sql1 = "SELECT DISTINCT d.e_oid as id FROM $tableProduce AS d WHERE (" . $arg->cond . ')' . $br . ' AND (d.deadline < ' . $date . ')' . "";
      //$sql1 = "SELECT DISTINCT d.e_oid as id FROM $tableProduce AS d WHERE ".$arg->cond." $br  AND d.deadline < '".$date."'";
      //$sql .= "  AND d.deadline < '".$date."' ";
      $stmt1 = $myPdo->prepare($sql1);
      $stmt1->execute();
      $smry[$arg->name]['urgent'] = $stmt1->rowCount();
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}

function smrydeadline($myPdo) {
  global $tableProduce;
  
  $args = MiscUtils::getParam('a', NULL);
  $args = ($args) ? json_decode($args) : array();
  $branch = json_decode(MiscUtils::getParam('b', NULL));
  
  if ($branch) {
    $br = " AND d.branch = '$branch'";
  } else {
    $br = '';
  }
  
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;
  $deadline = (SimpleDate::toStamp($now) - 259200);
  
  $smry = array();
  foreach ($args as &$arg) {
    try {
      $smry[$arg->name] = array();
      
      $sql = "SELECT DISTINCT d.e_oid AS total FROM $tableProduce AS d WHERE (" . $arg->cond . ')'. $br . ' AND (d.deadline < '  . $deadline . ')' . "";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $smry[$arg->name]['total'] = $stmt->rowCount();
      $smry[$arg->name]['urgent'] = $stmt->rowCount();
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}

function producepreconuser($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT p.e_oid as id, p.pi_model, p.trackId From $tableProduce as p WHERE $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    
    $tmp->preconUsers = array();
    $sql3 = "SELECT DISTINCT u.e_oid as id, u.* FROM $tableProduceUser AS u WHERE u.produce_id = $tmp->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $j = 0;
    while ($j < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp->preconUsers[] = $tmp3;
      $j++;
    }
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function customerproduce($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem, $tableProduceNote, $tableProducePurchaseItemSelect;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT * FROM $tableProduce as p WHERE $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->preconUsers = array();
    $sql1 = "SELECT * FROM $tableProduceUser AS u WHERE u.produce_id = $tmp->id";
    $stmt1 = $myPdo->prepare($sql1);
    $stmt1->execute();
    $j = 0;
    while ($j < $stmt1->rowCount()) {
      $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
      $tmp1->id = $tmp1->e_oid;
      $tmp->preconUsers[] = $tmp1;
      $j++;
    }
    $tmp->purchaseItems = array();
    $sql2 = "SELECT * FROM $tableProducePurchaseItem AS pi WHERE pi.produce_id = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp2->id = $tmp2->e_oid;
      $tmp2->selects = array();
      $sql21 = "SELECT DISTINCT pis.e_oid as id, pis.title, pis.value FROM $tableProducePurchaseItemSelect AS pis WHERE pis.purchase_id = $tmp2->id";
      $stmt21 = $myPdo->prepare($sql21);
      $stmt21->execute();
      $k = 0;
      while ($k < $stmt21->rowCount()) {
        $tmp21 = $stmt21->fetch(PDO::FETCH_OBJ);
        $tmp2->selects[] = $tmp21;
        $k++;
      }
      $tmp->purchaseItems[] = $tmp2;
      $j++;
    }
    $tmp->events = array();
    $sql3 = "SELECT * FROM $tableProduceEvent AS e WHERE e.produce_id = $tmp->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $j = 0;
    while ($j < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp3->id = $tmp3->e_oid;
      $tmp->events[] = $tmp3;
      $j++;
    }
    $tmp->tracks = array();
    $sql4 = "SELECT * FROM $tableProduceTrack AS t WHERE t.produce_id = $tmp->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $j = 0;
    while ($j < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp4->id = $tmp4->e_oid;
      $tmp4->preconUsers = array();
      $sql41 = "SELECT * FROM $tableProduceUser AS u WHERE u.ptrack_id = $tmp4->id";
      $stmt41 = $myPdo->prepare($sql41);
      $stmt41->execute();
      $k = 0;
      while ($k < $stmt41->rowCount()) {
        $tmp41 = $stmt41->fetch(PDO::FETCH_OBJ);
        $tmp41->id = $tmp41->e_oid;
        $tmp4->preconUsers[] = $tmp41;
        $k++;
      }
      $tmp->tracks[] = $tmp4;
      $j++;
    }
    $tmp->notes = array();
    $sql5 = "SELECT * FROM $tableProduceNote AS n WHERE n.produce_id = $tmp->id";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $j = 0;
    while ($j < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      $tmp5->id = $tmp5->e_oid;
      $tmp->notes[] = $tmp5;
      $j++;
    }
    $tmp->pi_selects = array();
    $sql6 = "SELECT * FROM $tableProducePurchaseItemSelect AS s WHERE s.produce_id = $tmp->id";
    $stmt6 = $myPdo->prepare($sql6);
    $stmt6->execute();
    $j = 0;
    while ($j < $stmt6->rowCount()) {
      $tmp6 = $stmt6->fetch(PDO::FETCH_OBJ);
      $tmp6->id = $tmp6->e_oid;
      $tmp->pi_selects[] = $tmp6;
      $j++;
    }
//    $tmp->produces = array();
//    $sql7 = "SELECT DISTINCT pr.e_oid as id, pr.* FROM $tableProduce AS pr WHERE pr.produce_id = $tmp->id";
//    $stmt7 = $myPdo->prepare($sql7);
//    $stmt7->execute();
//    $j = 0;
//    while ($j < $stmt7->rowCount()) {
//      $tmp7 = $stmt7->fetch(PDO::FETCH_OBJ);
//      $tmp->produces[] = $tmp7;
//      $j++;
//    }

    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}


function deadline($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem, $tableProduceNote, $tableProducePurchaseItemSelect;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'p.deadline');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $con = MiscUtils::getParam('line', NULL);
  if ($con == 1) {
    $now = SimpleDate::fromStamp(time() + LAG);
    $now->hours = 0;
    $now->minutes = 0;
    $now->seconds = 0;
    $deadline = (SimpleDate::toStamp($now) - 259200);
    $condition .= ' AND (p.deadline < \'' . $deadline . '\')';
  }
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (p.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (p.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $sql = "SELECT DISTINCT p.e_oid AS total FROM $tableProduce as p WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tableProduce as p WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->preconUsers = array();
    $sql1 = "SELECT * FROM $tableProduceUser AS u WHERE u.produce_id = $tmp->id";
    $stmt1 = $myPdo->prepare($sql1);
    $stmt1->execute();
    $j = 0;
    while ($j < $stmt1->rowCount()) {
      $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
      $tmp1->id = $tmp1->e_oid;
      $tmp->preconUsers[] = $tmp1;
      $j++;
    }
    $tmp->purchaseItems = array();
    $sql2 = "SELECT * FROM $tableProducePurchaseItem AS pi WHERE pi.produce_id = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp2->id = $tmp2->e_oid;
      $tmp2->selects = array();
      $sql21 = "SELECT DISTINCT pis.e_oid as id, pis.title, pis.value FROM $tableProducePurchaseItemSelect AS pis WHERE pis.purchase_id = $tmp2->id";
      $stmt21 = $myPdo->prepare($sql21);
      $stmt21->execute();
      $k = 0;
      while ($k < $stmt21->rowCount()) {
        $tmp21 = $stmt21->fetch(PDO::FETCH_OBJ);
        $tmp2->selects[] = $tmp21;
        $k++;
      }
      $tmp->purchaseItems[] = $tmp2;
      $j++;
    }
    $tmp->events = array();
    $sql3 = "SELECT * FROM $tableProduceEvent AS e WHERE e.produce_id = $tmp->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $j = 0;
    while ($j < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp3->id = $tmp3->e_oid;
      $tmp->events[] = $tmp3;
      $j++;
    }
    $tmp->tracks = array();
    $sql4 = "SELECT * FROM $tableProduceTrack AS t WHERE t.produce_id = $tmp->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $j = 0;
    while ($j < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp4->id = $tmp4->e_oid;
      $tmp4->preconUsers = array();
      $sql41 = "SELECT * FROM $tableProduceUser AS u WHERE u.ptrack_id = $tmp4->id";
      $stmt41 = $myPdo->prepare($sql41);
      $stmt41->execute();
      $k = 0;
      while ($k < $stmt41->rowCount()) {
        $tmp41 = $stmt41->fetch(PDO::FETCH_OBJ);
        $tmp41->id = $tmp41->e_oid;
        $tmp4->preconUsers[] = $tmp41;
        $k++;
      }
      $tmp->tracks[] = $tmp4;
      $j++;
    }
    $tmp->notes = array();
    $sql5 = "SELECT * FROM $tableProduceNote AS n WHERE n.produce_id = $tmp->id";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $j = 0;
    while ($j < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      $tmp5->id = $tmp5->e_oid;
      $tmp->notes[] = $tmp5;
      $j++;
    }
    $tmp->pi_selects = array();
    $sql6 = "SELECT * FROM $tableProducePurchaseItemSelect AS s WHERE s.produce_id = $tmp->id";
    $stmt6 = $myPdo->prepare($sql6);
    $stmt6->execute();
    $j = 0;
    while ($j < $stmt6->rowCount()) {
      $tmp6 = $stmt6->fetch(PDO::FETCH_OBJ);
      $tmp6->id = $tmp6->e_oid;
      $tmp->pi_selects[] = $tmp6;
      $j++;
    }
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function produce($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem, $tableProduceNote, $tableProducePurchaseItemSelect;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'p.deadline');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (p.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (p.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $sql = "SELECT DISTINCT p.e_oid AS total FROM $tableProduce as p WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tableProduce as p WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->preconUsers = array();
    $sql1 = "SELECT * FROM $tableProduceUser AS u WHERE u.produce_id = $tmp->id";
    $stmt1 = $myPdo->prepare($sql1);
    $stmt1->execute();
    $j = 0;
    while ($j < $stmt1->rowCount()) {
      $tmp1 = $stmt1->fetch(PDO::FETCH_OBJ);
      $tmp1->id = $tmp1->e_oid;
      $tmp->preconUsers[] = $tmp1;
      $j++;
    }
    $tmp->purchaseItems = array();
    $sql2 = "SELECT * FROM $tableProducePurchaseItem AS pi WHERE pi.produce_id = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp2->id = $tmp2->e_oid;
      $tmp2->selects = array();
      $sql21 = "SELECT DISTINCT pis.e_oid as id, pis.title, pis.value FROM $tableProducePurchaseItemSelect AS pis WHERE pis.purchase_id = $tmp2->id";
      $stmt21 = $myPdo->prepare($sql21);
      $stmt21->execute();
      $k = 0;
      while ($k < $stmt21->rowCount()) {
        $tmp21 = $stmt21->fetch(PDO::FETCH_OBJ);
        $tmp2->selects[] = $tmp21;
        $k++;
      }
      $tmp->purchaseItems[] = $tmp2;
      $j++;
    }
    $tmp->events = array();
    $sql3 = "SELECT * FROM $tableProduceEvent AS e WHERE e.produce_id = $tmp->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $j = 0;
    while ($j < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp3->id = $tmp3->e_oid;
      $tmp->events[] = $tmp3;
      $j++;
    }
    $tmp->tracks = array();
    $sql4 = "SELECT * FROM $tableProduceTrack AS t WHERE t.produce_id = $tmp->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $j = 0;
    while ($j < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp4->id = $tmp4->e_oid;
      $tmp4->preconUsers = array();
      $sql41 = "SELECT * FROM $tableProduceUser AS u WHERE u.ptrack_id = $tmp4->id";
      $stmt41 = $myPdo->prepare($sql41);
      $stmt41->execute();
      $k = 0;
      while ($k < $stmt41->rowCount()) {
        $tmp41 = $stmt41->fetch(PDO::FETCH_OBJ);
        $tmp41->id = $tmp41->e_oid;
        $tmp4->preconUsers[] = $tmp41;
        $k++;
      }
      $tmp->tracks[] = $tmp4;
      $j++;
    }
    $tmp->notes = array();
    $sql5 = "SELECT * FROM $tableProduceNote AS n WHERE n.produce_id = $tmp->id";
    $stmt5 = $myPdo->prepare($sql5);
    $stmt5->execute();
    $j = 0;
    while ($j < $stmt5->rowCount()) {
      $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
      $tmp5->id = $tmp5->e_oid;
      $tmp->notes[] = $tmp5;
      $j++;
    }
    $tmp->pi_selects = array();
    $sql6 = "SELECT * FROM $tableProducePurchaseItemSelect AS s WHERE s.produce_id = $tmp->id";
    $stmt6 = $myPdo->prepare($sql6);
    $stmt6->execute();
    $j = 0;
    while ($j < $stmt6->rowCount()) {
      $tmp6 = $stmt6->fetch(PDO::FETCH_OBJ);
      $tmp6->id = $tmp6->e_oid;
      $tmp->pi_selects[] = $tmp6;
      $j++;
    }
//    $tmp->produces = array();
//    $sql7 = "SELECT DISTINCT pr.e_oid as id, pr.* FROM $tableProduce AS pr WHERE pr.produce_id = $tmp->id";
//    $stmt7 = $myPdo->prepare($sql7);
//    $stmt7->execute();
//    $j = 0;
//    while ($j < $stmt7->rowCount()) {
//      $tmp7 = $stmt7->fetch(PDO::FETCH_OBJ);
//      $tmp->produces[] = $tmp7;
//      $j++;
//    }

    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}


function producenote($myPdo) {
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem, $tableProduceNote, $tableProducePurchaseItemSelect;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'n.oid');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 9999);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT * FROM $tableProduceNote AS n WHERE $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->date = ($tmp->date != 0) ? SimpleDate::fromStamp($tmp->date) : NULL;
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

?>